# CodeName：Paixu_Weihao
# Authon：liutao
# E-Mail：wxyj3496@126.com
# Creat time：2021-09-28
# Last Modified by:liutao
# Last Modified Time:2022-4-30
# futction：输入没有简化合并的元器件位号，进行简化。 
#			支持位号的格式如下：
#			1.符号+数字
#			2.符号+数字+符号
#			3.数字+符号+数字+符号
#			其余格式不合并,用正则表达式挑出来

# 导入pandas模块，用于拆分、排序、合并元器件位号
import pandas as pd
import re

# 正则表达式判断是否符合规则
def is_weihao(weihao,mode):
	pattern = re.compile(mode)
	result = pattern.match(weihao)

	if result:
		return True
	else:
		return False

# 函数主体
def jianhua_weihao(weihao_in):
	#新建一个3列的DataFrame，用于存拆分的位号
	df_wh = pd.DataFrame(columns=[0,1,2])
	# 将位号通过逗号分隔为字符串b，并计算字符串b的长度
	weihao_zong = weihao_in.split(',')
	weihao_fgeshi = []
	weihao_mgeshi = []
	str_fwh = ''

	# 正则表达式的规则
	geshi1 = r'^\d*\D+\d+\D*$'
	geshi2 = r'^\d*\D+0+'

	# 使用正则表达式，判断是否符合编号规则，将不符合的挑出来
	for m in weihao_zong:
		if is_weihao(m,geshi1):
			if is_weihao(m,geshi2):
				weihao_fgeshi.append(m)
			else:
				weihao_mgeshi.append(m)
		else:
			weihao_fgeshi.append(m)

	# 将不符合的位号合并为字符串
	if weihao_fgeshi != []:
		for i in weihao_fgeshi:
			if str_fwh == '':
				str_fwh = i
			else:
				str_fwh = str_fwh + ',' +i

	len_wh = len(weihao_mgeshi)

	# 对每个位号进行分隔，以数字为中心，数字前面为前缀，数字后面是后缀
	for d in weihao_mgeshi:
		#定义变量，x是前缀位置，y是中间数字尾缀，默认是最后1位的位置，n是循环过程中的位置,
		#m是标志位，m = 0 表示数字后没有字符，m = 1表示数字后有字符
		x = 0
		n = 0
		m = 0
		k = 0
		y = len(d)
		for l in d:
			if x == 0 and not(l.isdigit()):
				k = 1
			# 如果不是首个字符，检测到数字，则将数字首位位置赋值给x
			if k == 1 and x == 0 and l.isdigit():
				x = n
			# 如果检测到数字后有字符，将字符的首位置赋值给y,m赋值1(表示有尾缀)，否则y不变，仍等于字符长度
			elif x != 0 and not(l.isdigit()) and m == 0:
				y = n
				m = 1
			n = n + 1
		# 将字符分成前缀、序号、后缀三部分，若m = 1有尾缀，否则尾缀为空''
		if m == 1:
			e = pd.DataFrame([[d[:x],int(d[x:y]),d[y:]]])
		else:
			e = pd.DataFrame([[d[:x],int(d[x:y]),'']])

		#将分号的位号逐行合并到df_wh里
		df_wh = pd.concat([df_wh,e],axis = 0)
	
	#将列名改为'前缀','序号','后缀'
	df_wh.columns = ['前缀','序号','后缀']
	
	
	# 将df_wh排序，顺序由后到前，将序号从0递增
	df_wh.sort_values(['前缀','后缀','序号'], ascending = True, inplace = True)
	df_wh.reset_index(drop = True, inplace = True)

	
	#增加两列空列，'合并'用于存未合并的序号，'简化位号'用于存合并的序号
	df_wh.insert(3,'合并','')
	df_wh.insert(4,'简化位号','')
	# 将前缀后缀相同，位号连续的合并成一行，合并的存在'合并'列，并删除上一行
	m = 0
	for n in range(1,len_wh):
		if df_wh['前缀'][n-1] == df_wh['前缀'][n] and df_wh['后缀'][n-1] == df_wh['后缀'][n]\
			and int(df_wh['序号'][n]) - int(df_wh['序号'][n-1]) == 1:
			#对满足条件的位置合并
			if m == 0:
				df_wh.loc[n, '合并'] = str(df_wh.loc[n-1, '序号']) + ',' + str(df_wh.loc[n, '序号'])
				m = 1
			else:
				df_wh.loc[n, '合并'] = str(df_wh.loc[n-1, '合并']) + ',' + str(df_wh.loc[n, '序号'])
			# 删除第n-1列
			df_wh = df_wh.drop(n - 1)
		else:
			m = 0

	# 重新排序序号
	df_wh.reset_index(drop = True, inplace = True)
	
	# 对序号进行合并，2个用逗号分隔，3个以上用'-'相连，生成一个字符串
	str_wh = ''
	len_wh = len(df_wh)
	
	for n in range(0,len_wh):
		if df_wh['合并'][n] == '':
			b = df_wh['前缀'][n] + str(df_wh['序号'][n]) + df_wh['后缀'][n]
			if n == 0:
				str_wh = str_wh + b
			else:
				str_wh = str_wh + ',' + b
		else:
			a = str(df_wh['合并'][n]).split(',')
			m = len(a)
			if m == 2:
				b = df_wh['前缀'][n] + a[0] + df_wh['后缀'][n] + ','\
					+ df_wh['前缀'][n] + a[-1] + df_wh['后缀'][n]
			elif m > 2:
				b = df_wh['前缀'][n] + a[0] + df_wh['后缀'][n] + '-'\
					+ df_wh['前缀'][n] + a[-1] + df_wh['后缀'][n]
			if n == 0:
				str_wh = str_wh + b
			else:
				str_wh = str_wh + ',' + b
	if str_fwh != '':
		if str_wh == '':
			str_wh = str_fwh
		else:
			str_wh = str_wh + ',' + str_fwh
	return str_wh



if __name__ == '__main__':
	# weihao = '1C18A'
	weihao = 'X01,X02,X01G1,X01G2,XA,1,1a1a1a1a,128C18A,1C19A,1C20A,1C21A,2C22A,2C22B,2C23A,2C24A,C45,C44,C38,C37,C41,C40,C24,C17,C39,C34,C35,C36,C42,C43,C16,C14,C13,C12,C11A,C10,C9,C8,C7,C15'
	m = jianhua_weihao(weihao)
	print(m)


